﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoClassLibrary.Model;
using System.Data;
using System.IO;
using VideoClassLibrary.BusinessLogicLayer;

public partial class NewsManagement_UpdateNews : AdminUserBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
           
            trc.Visible = false;
            RadioButtonList1.Enabled = false;
            ShowNewHtml();
            showNews();
            showDdlCategory();
        } 

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        NewsModule nm = new NewsModule();
        V_News nv = new V_News();
        nv.ID = Convert.ToInt32(Request.QueryString["Nid"]);
        nv.SchoolId = this.CurrentUser.SchoolID;
        nv.UpdateTime = DateTime.Now;
        nv.NewsContent = this.edtrText.Text.ToString();
        nv.NewsAuthor = this.txtAuthor.Text;
         
        nv.IsPublished = Convert.ToInt32(this.RadioButtonList1.SelectedValue);
      
        nv.NewsTitle = this.txtNewsTitle.Text.ToString();
        nv.CategoryId = Convert.ToInt32(ddlCategory.SelectedValue.ToString());

        string fileExtension = string.Empty;
        if (!string.IsNullOrEmpty(this.fuNewsImage.FileName))//修改图片
        {
            fileExtension = System.IO.Path.GetExtension(this.fuNewsImage.FileName);
            if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
            {
                PageHelper.AlterMsg("图片格式只支持jpg/gif");
                return;
            }
            string dictionarySavedPath = PageHelper.GetWebConfigValue("SchoolImagePath");
            dictionarySavedPath += "\\Images\\SchoolId\\News\\";
            dictionarySavedPath = dictionarySavedPath.Replace("SchoolId", this.SchoolID.ToString());
            if (!Directory.Exists(dictionarySavedPath))
            {
                Directory.CreateDirectory(dictionarySavedPath);
            }
            // dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString());   
            //保存logo的文件夹绝对路径

            string logfileSavedPath = dictionarySavedPath +  nv.ID.ToString() + fileExtension;
            //fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
            this.fuNewsImage.SaveAs(logfileSavedPath);

            nv.NewsImage = ("/Images/SchoolId/News/" + nv.ID.ToString() + fileExtension).Replace("SchoolId", this.SchoolID.ToString());
        }
        nm.UpdateNews(nv);
        PageHelper.AlterMsgAndRedirect("修改成功！", "ViewNewsList.aspx");
    }

    private void showNews()
    {

        int nid =Convert.ToInt16(Request.QueryString["Nid"]);
        string sqlWhere = " id=" + nid+" and schoolid="+SchoolID;
        V_News ne=new V_News();
        NewsModule nm=new NewsModule();
        V_News model = nm.GetNewDetailByNewsId(nid);
        ddlCategory.SelectedValue = model.CategoryId.ToString();
        txtNewsTitle.Text = model.NewsTitle;
        this.edtrText.Text = model.NewsContent;
        if (AdminType == 4)
        {

            RadioButtonList1.SelectedValue = "0";
        }
        else
        {
            RadioButtonList1.SelectedValue = model.IsPublished.ToString();
        }
    }
    private void showDdlCategory()
    {

        NewsCategoryModule ncm = new NewsCategoryModule();
        int schoolid = this.CurrentUser.SchoolID;
        DataSet ds = ncm.QueryNewsCategoryByschool(schoolid);
        ddlCategory.DataSource = ds;
        ddlCategory.DataBind();
    }
    protected void btnreturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewNewsList.aspx");
    }

    private void ShowNewHtml()
    {
        DataSet ds = new DataSet();
        AdminModule am = new AdminModule();
        string typeva = AdminType.ToString();
        ds = am.GetAdmin(-1, typeva);
        DataRow dr = ds.Tables[0].Rows[0];
        int result = Convert.ToInt32(dr["VA_type"]);
        if (result != 4)
        {
            trc.Visible = true;
            RadioButtonList1.Enabled = true;
        }

    }
}
